package com.flyaudio.xiaopengautopilot.presenter;

import android.content.Context;
import android.os.Handler;
import android.util.Log;


public class VehicleModuleTest {
    Context context;
    private float perCToM = 5.8f;// 每度跑多少公里
    private float M_k = 0.24528f; //milge转单位米的常数

    private int delay=5000; //延时

    private  float power = 0f; //每次耗电多少度
    private float ret = 60.5f;//目标显示 ? 度/百公里
    private int milegeSept = 500; //每次叠加多少 milge
    private String TAG = "VehicleModuleTest";
    Handler handler = new Handler();
    int k = 10;
    Runnable task1 = new Runnable() {

        @Override
        public void run() {
            // handler自带方法实现定时器
//            Log.d("task1","run");
            computePerCToM();
            computeMilegeSeptAndPower();
            milege += milegeSept;
            byte[] data1 = new byte[]{(byte) 0x72,0x72>>8,0x72, (byte) (milege>>24), (byte) (milege>>16), (byte) (milege>>8), (byte) milege,12};

            IVehicleModule.getInstance().StandardFrame(data1);

            int i = (int) (power*10000);
            byte[] data = new byte[]{(byte) 0x80,0x00, (byte) (i>>24), (byte) (i>>16), (byte) (i>>8), (byte) i};
            Log.d(TAG, "run: " + i);
            IVehicleModule.getInstance().mcuExtendedData(data);

            data = new byte[]{(byte) 0x80,0x01, (byte) (k>>8), (byte) k};
            Log.d(TAG, "avg: " + k);
            IVehicleModule.getInstance().mcuExtendedData(data);
            k+=10;
            k%=80;
            handler.postDelayed(this, delay);//每隔3s执行
        }
    };
    int milege = 0;
    Runnable task2 = new Runnable() {

        @Override
        public void run() {
            // handler自带方法实现定时器
//            Log.d("task2","run");

            handler.postDelayed(this, delay);//每隔3s执行

        }
    };
    private void computePerCToM(){
        perCToM =100000/ ret; // x度/百公里 = x度/100000米 = x/100000 度/米  => 100000/x 米/度
    }
    private void computeMilegeSeptAndPower(){
        // c_m*milegeSept*M_k / Power = perCToM
        power = (milegeSept*M_k)/perCToM;  // k米 / x 米/度 = p度
    }
    VehicleModuleTest(Context context){
        this.context = context;
        computePerCToM();
        computeMilegeSeptAndPower();
        handler.postDelayed(task1, 1000);//延时多长时间启动定时器
//        handler.postDelayed(task2, 1000);//延时多长时间启动定时器
    }

}
